home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gigarom 1
/
Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso
/
FILES
/
DEV
/
C-H
/
CHexDmpDA.cpt
/
Hex Dump DA
/
window.HexDumpDA.c
< prev
Wrap
C/C++ Source or Header
|
1988-06-14
|
10KB
|
500 lines
#include <QuickDraw.h>
#include <WindowMgr.h>
#include <ControlMgr.h>
#include <FontMgr.h>
#include <DeviceMgr.h>
#include <FileMgr.h>
#include <SetUpA4.h>
#include "HexDump.h"
extern char buffer[];
extern WindowPtr wp;
extern long fileSize;
extern DCtlPtr dce; /* device control entry */
extern enum fork whichFork;
ControlHandle vScroll = (ControlHandle) 0L;
int curTop = 0;
int lineHeight;
int nLines;
long firstByte = -1L;
long linesInFile;
long sBegin = -1;
long sEnd = -1;
FontInfo fInfo;
#define TOPH 12
#define TOPV 3
long abs(i)
long i;
{
if (i<0) return(-i);
else return(i);
}
Scroll_Bits()
{
int oldTop;
RgnHandle tmpRgn, saveRgn;
long dv;
Rect r;
oldTop = curTop;
curTop = GetCtlValue( vScroll );
dv = (long)lineHeight * (oldTop-curTop);
r = wp->portRect;
r.left += TOPH;
r.top += TOPV + 2 + lineHeight;
r.right -= 16;
r.bottom = r.top + nLines*lineHeight;
tmpRgn=NewRgn();
saveRgn = NewRgn();
CopyRgn(wp->clipRgn, saveRgn);
if (dv) {
if (abs(dv)>=(long)lineHeight*nLines) {
EraseRect( &r );
RectRgn( tmpRgn, &r );
}
else {
saveRgn = NewRgn();
CopyRgn(wp->clipRgn, saveRgn);
ClipRect(&r);
ScrollRect( &r, 0, dv, tmpRgn );
SetClip(saveRgn);
DisposeRgn(saveRgn);
}
InvalRgn( tmpRgn );
}
DisposeRgn( tmpRgn );
}
max(a,b)
{
if (a>b) return(a);
else return(b);
}
/*
* doUpdate - draw our window's contents.
*
*/
doUpdate()
{
GrafPtr savePort;
Point p;
long first; /* offset of the first byte of "buffer" */
register int i;
RgnHandle tmpRgn;
Rect r, testRect, dummyRect;
char display[128];
GetPort( &savePort );
SetPort( wp );
nLines = ((wp->portRect.bottom - wp->portRect.top - TOPV - 17)/lineHeight) - 1;
SetCtlMax( vScroll, max( 0, (int)(linesInFile-nLines)) );
ReadFrom(first = 16L*GetCtlValue( vScroll ));
Scroll_Bits();
BeginUpdate( wp );
EraseRect(&wp->portRect);
DrawGrowIcon( wp );
DrawControls( wp );
/* save, then adjust the ClipRgn */
tmpRgn = NewRgn();
GetClip( tmpRgn );
r = wp->portRect;
r.right -= 15;
r.bottom -= 16;
ClipRect(&r);
Draw_Window_Header();
r.right -= 1;
ClipRect(&r);
TextFace(0);
MoveTo( TOPH, TOPV+2*lineHeight );
for (i=0; i<nLines; i++) {
/* check to see if line needs updating...*/
GetPen( &p );
SetRect(&testRect, p.h, p.v-fInfo.ascent,
wp->portRect.right, p.v+fInfo.descent);
if (RectInRgn( &testRect, wp->visRgn)) {
Fill_Line(first, &buffer[first-firstByte], display);
DrawString(display);
}
if (sBegin>0) Check_Select(first, true);
MoveTo( p.h, p.v+lineHeight );
first += 16;
}
/* put everything back */
SetClip(tmpRgn);
DisposeRgn(tmpRgn);
EndUpdate( wp );
SetPort( savePort );
}
New_Select(begin, end)
long begin, end;
{
GrafPtr savePort;
Point p;
long first = 16L*GetCtlValue( vScroll );
register int i;
RgnHandle tmpRgn;
Rect r;
GetPort( &savePort );
SetPort( wp );
first = 16L*GetCtlValue( vScroll );
nLines = ((wp->portRect.bottom - wp->portRect.top - TOPV - 17)/lineHeight) - 1;
/* save, then adjust the ClipRgn */
tmpRgn = NewRgn();
GetClip( tmpRgn );
r = wp->portRect;
r.right -= 16;
r.bottom -= 16;
ClipRect(&r);
MoveTo( TOPH, TOPV+2*lineHeight );
for (i=0; i<nLines; i++) {
/* check to see if selection is on this line...*/
GetPen( &p );
Check_Select(first, true);
MoveTo( p.h, p.v+lineHeight );
first += 16;
}
sBegin = begin;
sEnd = end;
first = 16L*GetCtlValue( vScroll );
MoveTo( TOPH, TOPV+2*lineHeight );
for (i=0; i<nLines; i++) {
/* check to see if selection is on this line...*/
GetPen( &p );
Check_Select(first, false);
MoveTo( p.h, p.v+lineHeight );
first += 16;
}
/* put everything back */
SetClip(tmpRgn);
DisposeRgn(tmpRgn);
SetPort( savePort );
}
Show_Point(n)
long n;
{
int new;
long delta;
GrafPtr savePort;
int max;
delta = (new=n/16) - GetCtlValue(vScroll);
if ((delta<0) || (delta>=nLines)) {
new -= nLines/2;
if (new<0) new = 0;
else if (new>(max=GetCtlMax(vScroll))) new = max;
SetCtlValue(vScroll, new);
GetPort(&savePort);
SetPort(wp);
InvalRect(&wp->portRect);
SetPort(savePort);
}
}
Draw_Window_Header()
{
Str255 s;
MoveTo( TOPH, TOPV+lineHeight-2 );
TextFace(bold);
NumToString( fileSize, s );
DrawString("\pEOF = ");
DrawString(s);
DrawString("\p ($");
Num_To_Hex( fileSize, s );
DrawString(s);
DrawString("\p)");
if (whichFork==data) pstrcpy(s, "\pData fork");
else pstrcpy(s, "\pResource fork");
MoveTo( wp->portRect.right-23-StringWidth(s), TOPV+lineHeight-2 );
DrawString(s);
MoveTo( wp->portRect.right-15, TOPV+lineHeight );
LineTo( wp->portRect.left, TOPV+lineHeight );
}
static beforeCoord1[16] = { 9, 11, 14, 16, 19, 21, 24, 26,
30, 32, 35, 37, 40, 42, 45, 47 };
static afterCoord1[16] = { 11, 13, 16, 18, 21, 23, 26, 28,
32, 34, 37, 39, 42, 44, 47, 49 };
#define before2 52
Check_Select(first, drawit)
long first;
Boolean drawit;
{
register int w = CharWidth('0');
Point p;
Rect r;
int left1, left2, right1, right2;
if (sBegin<first) {
if (sEnd<=first) return;
left1 = TOPH + 9*w;
left2 = TOPH + before2*w;
}
else if (sBegin<first+16) {
left1 = TOPH + beforeCoord1[sBegin-first]*w;
left2 = TOPH + (before2 + sBegin - first)*w;
}
else return;
if (sEnd<first+16) {
right1 = TOPH + afterCoord1[sEnd-first-1]*w;
right2 = TOPH + (before2 + sEnd - first)*w;
}
else {
right1 = TOPH + afterCoord1[15]*w;
right2 = TOPH + (before2+16)*w;
}
GetPen(&p);
SetRect(&r, left1-1, p.v-fInfo.ascent, right1, p.v+fInfo.descent);
if (drawit) InvertRect(&r);
else InvalRect(&r);
SetRect(&r, left2-1, p.v-fInfo.ascent, right2, p.v+fInfo.descent);
if (drawit) InvertRect(&r);
else InvalRect(&r);
}
/*
* ScrollProc - the call-back routine to handle scrolling
*
* This routine is called from the OS routine TrackControl. As such,
* it must be declared "pascal". It also references data global to
* the DA, and thus must SetUpA4 before updating the window's
* appearance. Finally, it return to TrackControl, so it must
* RestoreA4 before going back.
*/
int scrollCode;
int scrollAmt;
pascal void ScrollProc(theControl, theCode)
ControlHandle theControl;
int theCode;
{
int locVal;
GrafPtr theWindow;
SetUpA4();
if (theCode==scrollCode) {
locVal = GetCtlValue(theControl);
SetCtlValue( theControl, locVal+scrollAmt );
doUpdate();
}
RestoreA4();
}
doScrollControl(p)
Point p;
{
GrafPtr savePort;
int code,v;
ControlHandle theControl;
RememberA4();
GetPort( &savePort );
SetPort( wp );
GlobalToLocal(&p);
switch ( code = FindControl(p, wp, &theControl) ) {
case inUpButton: scrollAmt = -1; break;
case inDownButton: scrollAmt = 1; break;
case inPageUp: scrollAmt = -nLines+1; break;
case inPageDown: scrollAmt = nLines-1; break;
default: ;
}
if (theControl==vScroll) {
switch (code) {
case inUpButton:
case inDownButton:
case inPageUp:
case inPageDown:
scrollCode = code;
if (TrackControl(vScroll, p, &ScrollProc)) ;
break;
case inThumb:
if (TrackControl(theControl, p, 0L )) ;
doUpdate();
break;
default: ;
}
}
SetPort( savePort );
}
/* Activate the DA's window */
doActivate(activate)
Boolean activate;
{
Rect r;
GrafPtr savePort;
GetPort(&savePort);
SetPort(wp);
r = wp->portRect;
r.top = r.bottom - 16;
r.left = r.left - 16;
InvalRect(&r);
if ( activate ) ShowControl( vScroll );
else HideControl( vScroll );
SetPort(savePort);
}
doGrow(p)
Point p;
{
GrafPtr savePort;
long theResult;
Point thePt;
Rect r;
static Rect wBounds = { 80, 300, 32767, 444 };
GetPort( &savePort );
SetPort( wp );
theResult = GrowWindow( wp, p, &wBounds );
thePt.h = LoWord( theResult );
thePt.v = HiWord( theResult );
thePt.v = lineHeight*(thePt.v/lineHeight);
r = wp->portRect;
r.left = r.right-16;
InvalRect(&r);
r.left = wp->portRect.left;
r.top = r.bottom-16;
InvalRect(&r);
SizeWindow( wp, thePt.h, thePt.v, 1 );
r = wp->portRect;
r.left = r.right-16;
MoveControl( vScroll, r.left+1, r.top-1);
SizeControl( vScroll, 16, r.bottom-r.top-13);
InvalRect(&r);
r.left = wp->portRect.left;
r.top = r.bottom-16;
InvalRect(&r);
r = wp->portRect;
r.bottom = r.top + TOPV + lineHeight-2;
InvalRect(&r);
SetPort( savePort );
}
doZoom(p,msg)
Point p;
int msg;
{
GrafPtr savePort;
Rect r;
GetPort(&savePort);
SetPort(wp);
if (TrackBox(wp,p,msg)) {
EraseRect(&wp->portRect);
ZoomWindow(wp,msg,false);
r = wp->portRect;
r.left = r.right-16;
MoveControl( vScroll, r.left+1, r.top-1);
SizeControl( vScroll, 16, r.bottom-r.top-13);
}
SetPort(savePort);
}
/* New_File_Window - open the window on a new file */
New_File_Window(size, name)
long size;
char *name;
{
GrafPtr savePort;
linesInFile = (fileSize-1)/16 + 1;
firstByte = -1; /* to force a re-read */
GetPort(&savePort);
SetWTitle( wp, name );
ShowWindow( wp );
SelectWindow( wp );
SetPort(wp);
InvalRect( &wp->portRect );
SetCtlValue(vScroll, 0);
SetPort(savePort);
}
/* Create_Window - create our HexDump window */
#define WINDOWWIDTH 444
#define WINDOWHEIGHT 276
#define WINDOWTOP 40
#define WINDOWLEFT 10
#define WINDOWBOTTOM WINDOWTOP + WINDOWHEIGHT
#define WINDOWRIGHT WINDOWLEFT + WINDOWWIDTH
#define SBARWIDTH 16
#define SBARLEFT WINDOWWIDTH - SBARWIDTH + 1
#define SBARRIGHT WINDOWWIDTH + 1
#define SBARBOTTOM WINDOWHEIGHT - SBARWIDTH + 2
Create_Window()
{
GrafPtr savePort;
static Rect bounds = { WINDOWTOP, WINDOWLEFT,
WINDOWBOTTOM, WINDOWRIGHT },
scrollBounds = { -1, SBARLEFT,
SBARBOTTOM, SBARRIGHT };
GetPort(&savePort);
wp = NewWindow( 0L, &bounds, "", FALSE, 8, 0L, TRUE, 0L );
/* IMPORTANT - set the windowkind to the dCtlRefNum */
((WindowPeek)wp)->windowKind = dce->dCtlRefNum;
dce->dCtlWindow = wp; /* courtesy of TLL */
/* set up the window's font and line height: */
SetPort(wp);
TextFont(monaco);
TextSize(9);
GetFontInfo(&fInfo);
lineHeight = fInfo.ascent+fInfo.descent+fInfo.leading;
/* set up the scroll bar */
vScroll = NewControl( wp, &scrollBounds, "", true, 0, 0, 0,
scrollBarProc, 0L );
SetPort(savePort);
}
Destroy_Window()
{
if (vScroll) DisposeControl( vScroll );
if (wp) {
DisposeWindow( wp );
wp = 0L;
dce->dCtlWindow = 0; /* courtesy of TLL */
}
}